package com.model.houseon;

import java.util.ArrayList;


import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import com.controller.houseon.ControlHouseON;


public class ComunicaWebService {
	
	String NAMESPACE;
	String METHOD_NAME;
	String SOAP_ACTION;
	String URL;
	
	public ComunicaWebService(String METHOD_NAME){
		this.NAMESPACE = "http://tempuri.org/";
		this.METHOD_NAME = METHOD_NAME;
		this.SOAP_ACTION = this.NAMESPACE + METHOD_NAME;
		this.URL = "http://66.85.130.178/hms/WSHouseOn.asmx?WSDL";
		//this.URL = "http://192.168.1.33/WSHouseOn.asmx";
		
	}
	
	
	public UsuarioBean ValidarUsuario(String user, String pass){
		
		// TODO Auto-generated method stub
				UsuarioBean usuario=null;
			        //create a SOAP object
		        SoapObject request = new SoapObject("http://tempuri.org/", "ValidaUsuarioWS");
		        //add method arguments
		        request.addProperty("usuario", user);
		        request.addProperty("password", pass);

		        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		        envelope.dotNet = true;
		        envelope.setOutputSoapObject(request);

		        HttpTransportSE transport = new HttpTransportSE(URL);

		        try {

		        	transport.call(SOAP_ACTION, envelope);
		            SoapObject content = (SoapObject) envelope.bodyIn;
		            
		            SoapObject usuarioSoapObjet = (SoapObject)content.getProperty(0);
		            SoapObject tipoUSoapObjet = (SoapObject)usuarioSoapObjet.getProperty(9);
		         
		            usuario= new UsuarioBean();
		            usuario.setIdUsuario(Integer.parseInt(usuarioSoapObjet.getProperty("ID").toString()));
		            usuario.setNombres(usuarioSoapObjet.getProperty("Nombre").toString()+" "+usuarioSoapObjet.getProperty("Apellidos").toString());
		            usuario.getTipoUsuario().setIdTipoUsuario(((Integer.parseInt(tipoUSoapObjet.getProperty("ID").toString()))));
		            usuario.setEmail(usuarioSoapObjet.getProperty("Email").toString());
		            usuario.setEstado(Boolean.parseBoolean(usuarioSoapObjet.getProperty("Estado").toString()));

		        } catch (Exception e) {
		            e.printStackTrace();
		        }
		
		return usuario;
	}
	
	public ArrayList<CasaBean> ListarCasa(int idUsuario){
		
	
		ArrayList<CasaBean> lstCasa=new ArrayList<CasaBean>();
		
		SoapObject request= new SoapObject(this.NAMESPACE, "ListaCasaWS");
		request.addProperty("idUsuario", idUsuario);
		
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
	    envelope.dotNet = true;
	    envelope.setOutputSoapObject(request);

	    HttpTransportSE transport = new HttpTransportSE(URL);
	    try {
	    	
	    	transport.call(SOAP_ACTION, envelope);
            SoapObject content = (SoapObject) envelope.bodyIn;
            SoapObject lstCasaSoapObjet = (SoapObject)content.getProperty(0);
            
            for (int i = 0; i < lstCasaSoapObjet.getPropertyCount(); i++) {
            	SoapObject casaSoapObjet = (SoapObject)lstCasaSoapObjet.getProperty(i);
            	//SoapObject listTipo=(SoapObject)list.getProperty(1);
            	CasaBean objCasa= new CasaBean();
            	objCasa.setIdCasa(Integer.parseInt(casaSoapObjet.getProperty("ID").toString()));
            	objCasa.setDescripcion(casaSoapObjet.getProperty("Nombre").toString());
            	objCasa.setFoto(casaSoapObjet.getProperty("Imagen").toString());
            	objCasa.setEstado(Boolean.parseBoolean(casaSoapObjet.getProperty("Estado").toString()));
            	
            	lstCasa.add(objCasa);
            	
	    }
	    
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

		
		return lstCasa;
	}
	
	public CasaBean BuscarCasaxId(int idCasa){
		
		
		CasaBean objCasa=null;
		
		SoapObject request= new SoapObject(this.NAMESPACE, "BuscarCasaxIdWS");
		request.addProperty("idCasa", idCasa);
		
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
	    envelope.dotNet = true;
	    envelope.setOutputSoapObject(request);

	    HttpTransportSE transport = new HttpTransportSE(URL);
	    try {
	    	
	    	transport.call(SOAP_ACTION, envelope);
            SoapObject content = (SoapObject) envelope.bodyIn;
            SoapObject casaSoapObjet = (SoapObject)content.getProperty(0);
            
            objCasa= new CasaBean();
            objCasa.setIdCasa(Integer.parseInt(casaSoapObjet.getProperty("ID").toString()));
           	objCasa.setDescripcion(casaSoapObjet.getProperty("Nombre").toString());
           	objCasa.setIp(casaSoapObjet.getProperty("Ip").toString());
           	//objCasa.setUrl(casaSoapObjet.getProperty("url").toString());
           	objCasa.setPuerto(casaSoapObjet.getProperty("Puerto").toString());
           	objCasa.setFoto(casaSoapObjet.getProperty("foto").toString());
           	objCasa.setEstado(Boolean.parseBoolean(casaSoapObjet.getProperty("Estado").toString()));
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

		
		return objCasa;
	}
	
	public ArrayList<TipoEscenarioBean> ListaTipoEscenario(){
		
		
		ArrayList<TipoEscenarioBean> lstTipoE=new ArrayList<TipoEscenarioBean>();
		
		SoapObject request= new SoapObject(this.NAMESPACE, "ListaTipoEscenarioWS");
		//request.addProperty("idUsuario", idUsuario);
		
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
	    envelope.dotNet = true;
	    envelope.setOutputSoapObject(request);

	    HttpTransportSE transport = new HttpTransportSE(URL);
	    try {
	    	
	    	transport.call(SOAP_ACTION, envelope);
            SoapObject content = (SoapObject) envelope.bodyIn;
            SoapObject lstTipoESoapObjet = (SoapObject)content.getProperty(0);
            
            for (int i = 0; i < lstTipoESoapObjet.getPropertyCount(); i++) {
            	SoapObject tipoESoapObjet = (SoapObject)lstTipoESoapObjet.getProperty(i);
            	
            	TipoEscenarioBean objTipoE= new TipoEscenarioBean();
            	objTipoE.setIdTipoEscenario(Integer.parseInt(tipoESoapObjet.getProperty("ID").toString()));
            	objTipoE.setNombre(tipoESoapObjet.getProperty("Nombre").toString());
            	objTipoE.setEstado(Boolean.parseBoolean(tipoESoapObjet.getProperty("Estado").toString()));
            	objTipoE.setImagen(tipoESoapObjet.getProperty("Imagen").toString());
            	lstTipoE.add(objTipoE);
	    }
	    
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	    
		return lstTipoE;
	}
	
	
	public ArrayList<TipoDispositivoBean> ListaTipoDispostivo(){
			
			
			ArrayList<TipoDispositivoBean> lstTipoD=new ArrayList<TipoDispositivoBean>();
			
			SoapObject request= new SoapObject(this.NAMESPACE, "ListaTipoDispositivoWS");
			//request.addProperty("idUsuario", idUsuario);
			
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		    envelope.dotNet = true;
		    envelope.setOutputSoapObject(request);
	
		    HttpTransportSE transport = new HttpTransportSE(URL);
		    try {
		    	
		    	transport.call(SOAP_ACTION, envelope);
	            SoapObject content = (SoapObject) envelope.bodyIn;
	            SoapObject lstTipoDSoapObjet = (SoapObject)content.getProperty(0);
	            
	            for (int i = 0; i < lstTipoDSoapObjet.getPropertyCount(); i++) {
	            	SoapObject tipoESoapObjet = (SoapObject)lstTipoDSoapObjet.getProperty(i);
	            	
	            	TipoDispositivoBean objTipoD= new TipoDispositivoBean();
	            	objTipoD.setIdTipoDispositivo(Integer.parseInt(tipoESoapObjet.getProperty("ID").toString()));
	            	objTipoD.setNombre(tipoESoapObjet.getProperty("Nombre").toString());
	            	objTipoD.setEstado(Boolean.parseBoolean(tipoESoapObjet.getProperty("Estado").toString()));
	            	objTipoD.setImagen(tipoESoapObjet.getProperty("Imagen").toString());
	            	lstTipoD.add(objTipoD);
		    }
		    
				
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		    
			return lstTipoD;
		}
	
	
	public ArrayList<EscenarioBean> ListaEscenarioxTipoE(int idTipoEscenario , int idCasa){
		
		
		ArrayList<EscenarioBean> lstEscenario=new ArrayList<EscenarioBean>();
		
		SoapObject request= new SoapObject(this.NAMESPACE, "ListaEscenarioxTipoxCasaWS");
		request.addProperty("idTipo", idTipoEscenario);
		request.addProperty("idCasa", idCasa);
		
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
	    envelope.dotNet = true;
	    envelope.setOutputSoapObject(request);
	
	    HttpTransportSE transport = new HttpTransportSE(URL);
	    try {
	    	
	    	transport.call(SOAP_ACTION, envelope);
	        SoapObject content = (SoapObject) envelope.bodyIn;
	        SoapObject lstESoapObjet = (SoapObject)content.getProperty(0);
	        
	        
	        for (int i = 0; i < lstESoapObjet.getPropertyCount(); i++) {
	        	SoapObject esenarioSoapObjet = (SoapObject)lstESoapObjet.getProperty(i);
	        	SoapObject tipoESoapObjet = (SoapObject)esenarioSoapObjet.getProperty(8);
	        	
	        	EscenarioBean objEscenario= new EscenarioBean();
	        	objEscenario.setIdEscenario(Integer.parseInt(esenarioSoapObjet.getProperty("ID").toString()));
	        	
	        	TipoEscenarioBean t= new TipoEscenarioBean();
	        	t.setNombre(tipoESoapObjet.getProperty("Nombre").toString());
	        	objEscenario.setTipoEscenario(t);
	        	objEscenario.setNombre(esenarioSoapObjet.getProperty("Nombre").toString());
	        	objEscenario.setImagen(esenarioSoapObjet.getProperty("Imagen").toString());
	        	objEscenario.setEstado(Boolean.parseBoolean(esenarioSoapObjet.getProperty("Estado").toString()));
	        	
	        	lstEscenario.add(objEscenario);
	    }
	    
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	    
		return lstEscenario;
	}
	
	//////// este metodo tiene que mostrar todo.. por analizar :)
	
	public ArrayList<DEscenarioBean> ListaDispositivoxTipoxCasa(int idTipo, int idCasa){
		
		
		ArrayList<DEscenarioBean> lstDEscenario=new ArrayList<DEscenarioBean>();
		
		SoapObject request= new SoapObject(this.NAMESPACE, "ListaDispositivoxTipoxCasaWS");
		request.addProperty("idTipo", idTipo);
		request.addProperty("idCasa", idCasa);
		
		
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
	    envelope.dotNet = true;
	    envelope.setOutputSoapObject(request);
	
	    HttpTransportSE transport = new HttpTransportSE(URL);
	    try {
	    	
	    	transport.call(SOAP_ACTION, envelope);
	        SoapObject content = (SoapObject) envelope.bodyIn;
	        SoapObject lstDEscenarioSoapObjet = (SoapObject)content.getProperty(0);
	        
	        	
	        
	        for (int i = 0; i < lstDEscenarioSoapObjet.getPropertyCount(); i++) {
	        	
	        	
	        	SoapObject itemDESoapObjet = (SoapObject)lstDEscenarioSoapObjet.getProperty(i);
	        	SoapObject escSoapObjet = (SoapObject)itemDESoapObjet.getProperty(8);
	        	SoapObject dispSoapObjet = (SoapObject)itemDESoapObjet.getProperty(11);
	        	
	        	DEscenarioBean objDEsenario= new DEscenarioBean();
	        	
	        	objDEsenario.setIdDEscenario(Integer.parseInt(itemDESoapObjet.getProperty("ID").toString()));
	        	objDEsenario.setNombre(itemDESoapObjet.getProperty("Nombre").toString());
	        	objDEsenario.setEstadoEncendido(Boolean.parseBoolean(itemDESoapObjet.getProperty("EstadoEncendido").toString()));
	        	objDEsenario.setEstadoDisponibilidad(Boolean.parseBoolean(itemDESoapObjet.getProperty("EstadoDisponibilidad").toString()));
	        
	        	EscenarioBean e= new EscenarioBean();
	        	e.setIdEscenario(Integer.parseInt(escSoapObjet.getProperty("ID").toString()));
	        	e.setNombre(escSoapObjet.getProperty("Nombre").toString());
	        	objDEsenario.setEscenario(e);
	        	
	        	DispositivoBean dip= new DispositivoBean();
	        	dip.setImagen(dispSoapObjet.getProperty("Imagen").toString());
	        	
	        	if(dip.getImagen().equals("camaras"))
	        		objDEsenario.setCamara(true);
	        	
	        	objDEsenario.setDispositivo(dip);
	        	
	        	lstDEscenario.add(objDEsenario);
	    }
	    
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	    
		return lstDEscenario;
	 }
	
	
	////////////////
	
	public ArrayList<DEscenarioBean> ListaDispositivoxEscenario(int idEscenario){
		
		
		ArrayList<DEscenarioBean> lstDEscenario=new ArrayList<DEscenarioBean>();
		
		SoapObject request= new SoapObject(this.NAMESPACE, "ListaDispositivoxEscenarioWS");
		request.addProperty("idEscenario", idEscenario);
		
		
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
	    envelope.dotNet = true;
	    envelope.setOutputSoapObject(request);
	
	    HttpTransportSE transport = new HttpTransportSE(URL);
	    try {
	    	
	    	transport.call(SOAP_ACTION, envelope);
	        SoapObject content = (SoapObject) envelope.bodyIn;
	        SoapObject lstDEscenarioSoapObjet = (SoapObject)content.getProperty(0);
	        
	        	
	        
	        for (int i = 0; i < lstDEscenarioSoapObjet.getPropertyCount(); i++) {
	        	
	        	
	        	SoapObject itemDESoapObjet = (SoapObject)lstDEscenarioSoapObjet.getProperty(i);
	        	SoapObject dispSoapObjet = (SoapObject)itemDESoapObjet.getProperty(10);
	        	
	        	DEscenarioBean objDEsenario= new DEscenarioBean();
	        	
	        	objDEsenario.setIdDEscenario(Integer.parseInt(itemDESoapObjet.getProperty("ID").toString()));
	        	objDEsenario.setNombre(itemDESoapObjet.getProperty("Nombre").toString());
	        	objDEsenario.setEstadoEncendido(Boolean.parseBoolean(itemDESoapObjet.getProperty("EstadoEncendido").toString()));
	        	objDEsenario.setEstadoDisponibilidad(Boolean.parseBoolean(itemDESoapObjet.getProperty("EstadoDisponibilidad").toString()));
	        	
	        	DispositivoBean d= new DispositivoBean();
	        	d.setIdDispositivo(Integer.parseInt(dispSoapObjet.getProperty("ID").toString()));
	        	d.setImagen(dispSoapObjet.getProperty("Imagen").toString());
	        	
	        	if(d.getImagen().equals("camaras"))
	        		objDEsenario.setCamara(true);
	 		    
	        	EscenarioBean e= new EscenarioBean();
	        	e.setIdEscenario(idEscenario);
	        	//e.setNombre(nombre);
	        	
	        	objDEsenario.setDispositivo(d);
	        	objDEsenario.setEscenario(e);
	        	
	        	lstDEscenario.add(objDEsenario);
	    }
	    
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	    
		return lstDEscenario;
	 }
	
	public Boolean ActualizaEstadoD(int idDispositivo) {
		// TODO Auto-generated method stub
		boolean band= false;
		SoapObject request = new SoapObject("http://tempuri.org/", "ActualizarEstadoDispWS");
	    //add method arguments
	    request.addProperty("idDetDisp", idDispositivo);
	
	    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
	    envelope.dotNet = true;
	    envelope.setOutputSoapObject(request);
	   
	    HttpTransportSE transport = new HttpTransportSE(URL);
	    try {
	    	
	    	transport.call(SOAP_ACTION, envelope);
	        SoapObject content = (SoapObject) envelope.bodyIn;
	        ControlHouseON.getInstancia().CurrentEstado = Boolean.parseBoolean(content.getProperty(0).toString());
	       	band=true;
	
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			band =false;
		}
		return band;
	}
	
	public Boolean ActualizarCasaWS(int idcasa,String nombreCasa,String img) {
		
		// TODO Auto-generated method stub
		boolean band= false;
		SoapObject request = new SoapObject("http://tempuri.org/","ActualizarCasaWS");
	    //add method arguments
	    request.addProperty("idCasa", idcasa);
	    request.addProperty("nombreCasa", nombreCasa);
	    request.addProperty("imgCasa", img);
	
	    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
	    envelope.dotNet = true;
	    envelope.setOutputSoapObject(request);
	   
	    HttpTransportSE transport = new HttpTransportSE(URL);
	    try {
	    	
	    	transport.call(SOAP_ACTION, envelope);
	        SoapObject content = (SoapObject) envelope.bodyIn;
	        band=Boolean.parseBoolean(content.getProperty(0).toString());
	       	//band=true;
	
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			band =false;
		}
		return band;
	}
	
public Boolean ActualizarEscenarioWS(int idEscenario,String nombreEscenario,String img) {
		
		// TODO Auto-generated method stub
		boolean band= false;
		SoapObject request = new SoapObject("http://tempuri.org/","ActualizarEscenarioWS");
	    //add method arguments
	    request.addProperty("id", idEscenario);
	    request.addProperty("nombre", nombreEscenario);
	    request.addProperty("img", img);
	
	    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
	    envelope.dotNet = true;
	    envelope.setOutputSoapObject(request);
	   
	    HttpTransportSE transport = new HttpTransportSE(URL);
	    try {
	    	
	    	transport.call(SOAP_ACTION, envelope);
	        SoapObject content = (SoapObject) envelope.bodyIn;
	        band=Boolean.parseBoolean(content.getProperty(0).toString());
	       	//band=true;
	
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			band =false;
		}
		return band;
	}


public Boolean ActualizarDetWS(int idDetalle,String nombreDetalle) {
	
	// TODO Auto-generated method stub
	boolean band= false;
	SoapObject request = new SoapObject("http://tempuri.org/","ActualizarNombreDetWS");
    //add method arguments
    request.addProperty("id", idDetalle);
    request.addProperty("nombre", nombreDetalle);
    

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);
   
    HttpTransportSE transport = new HttpTransportSE(URL);
    try {
    	
    	transport.call(SOAP_ACTION, envelope);
        SoapObject content = (SoapObject) envelope.bodyIn;
        band=Boolean.parseBoolean(content.getProperty(0).toString());
       	//band=true;

	} catch (Exception e) {
		// TODO: handle exception
		e.printStackTrace();
		band =false;
	}
	return band;
}


public Boolean ActualizarDispositivoWS(int idDisp,String nombreDisp, String img) {
	
	// TODO Auto-generated method stub
	boolean band= false;
	SoapObject request = new SoapObject("http://tempuri.org/","ActualizarDispositivoWS");
    //add method arguments
    request.addProperty("id", idDisp);
    request.addProperty("nombre", nombreDisp);
    request.addProperty("img", img);
    

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);
   
    HttpTransportSE transport = new HttpTransportSE(URL);
    try {
    	
    	transport.call(SOAP_ACTION, envelope);
        SoapObject content = (SoapObject) envelope.bodyIn;
        band=Boolean.parseBoolean(content.getProperty(0).toString());
       	//band=true;

	} catch (Exception e) {
		// TODO: handle exception
		e.printStackTrace();
		band =false;
	}
	return band;
}
	
	

	
}
